Open In Colab Weave는 Cohere Python library를 통해 이루어진 LLM 호출을 weave.init() 호출 후 자동으로 추적하고 기록합니다.

추적

개발 중이나 프로덕션 환경에서 LLM 애플리케이션의 추적을 중앙 데이터베이스에 저장하는 것이 중요합니다. 이러한 추적은 디버깅에 사용되며, 애플리케이션을 개선하는 데 도움이 되는 데이터셋으로 활용됩니다. Weave는 다음에 대한 추적을 자동으로 캡처합니다 cohere-python. 라이브러리를 평소처럼 사용할 수 있으며, weave.init()를 호출하여 시작하세요:
import cohere
import os
import weave

# Use the Cohere library as usual
co = cohere.Client(api_key=os.environ["COHERE_API_KEY"])

# highlight-next-line
weave.init("cohere_project")

response = co.chat(
    message="How is the weather in Boston?",
    # perform web search before answering the question. You can also use your own custom connector.
    connectors=[{"id": "web-search"}],
)
print(response.text)
cohere 모델의 강력한 기능 중 하나는 connectors를 사용하여 엔드포인트 측에서 다른 API에 요청을 할 수 있다는 것입니다. 응답에는 커넥터에서 반환된 문서에 연결되는 인용 요소가 포함된 생성된 텍스트가 포함됩니다. cohere_trace.png
우리는 Cohere Client.chat, AsyncClient.chat, Client.chat_stream, 그리고 AsyncClient.chat_stream 메서드를 패치하여 LLM 호출을 추적합니다.

자신만의 ops로 래핑하기

Weave ops는 결과를 reproducible하게 만들어 실험하면서 코드를 자동으로 버전 관리하고, 입력과 출력을 캡처합니다. 간단히 @weave.op()로 장식된 함수를 만들어 Cohere의 채팅 메서드를 호출하면, Weave가 입력과 출력을 추적합니다. 다음은 예시입니다:
import cohere
import os
import weave

co = cohere.Client(api_key=os.environ["COHERE_API_KEY"])

weave.init("cohere_project")

# highlight-next-line
@weave.op()
def weather(location: str, model: str) -> str:
    response = co.chat(
        model=model,
        message=f"How is the weather in {location}?",
        # perform web search before answering the question. You can also use your own custom connector.
        connectors=[{"id": "web-search"}],
    )
    return response.text

print(weather("Boston", "command"))
cohere_ops.png

더 쉬운 실험을 위해 Model를 만드세요

여러 요소가 있을 때 실험을 구성하기는 어렵습니다. Model 클래스를 사용하면 시스템 프롬프트나 사용 중인 모델과 같은 앱의 실험 세부 정보를 캡처하고 구성할 수 있습니다. 이는 앱의 다양한 반복을 구성하고 비교하는 데 도움이 됩니다. 코드 버전 관리 및 입력/출력 캡처 외에도, Model는 애플리케이션의 동작을 제어하는 구조화된 매개변수를 캡처하여 어떤 매개변수가 가장 잘 작동했는지 쉽게 찾을 수 있게 합니다. Weave 모델을 serve, 및 Evaluation와 함께 사용할 수도 있습니다. 아래 예시에서는 modeltemperature로 실험할 수 있습니다. 이 중 하나를 변경할 때마다 새로운 versionWeatherModel가 생성됩니다.
import weave
import cohere
import os

weave.init('weather-cohere')

class WeatherModel(weave.Model):
    model: str
    temperature: float
  
    @weave.op()
    def predict(self, location: str) -> str:
        co = cohere.Client(api_key=os.environ["COHERE_API_KEY"])
        response = co.chat(
            message=f"How is the weather in {location}?",
            model=self.model,
            temperature=self.temperature,
            connectors=[{"id": "web-search"}]
        )
        return response.text

weather_model = WeatherModel(
    model="command",
    temperature=0.7
)
result = weather_model.predict("Boston")
print(result)
cohere_model.png